package com.situ.mydictionary;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.situ.mydatabase.MyDatabaseHelper;

public class Main_Activity extends AppCompatActivity {

    private Button b1, b2, b3, change, write, del;
    private MyDatabaseHelper mydbhelper;
    private TextView txt;
    private int cid;

    //fragment
    private TranslateActivity trans;
    private DictionaryActivity dic;




    private void initView() {
        change = (Button)findViewById(R.id.change);
        write = (Button)findViewById(R.id.write);
        txt = (TextView)findViewById(R.id.text_view);



        del = (Button)findViewById(R.id.del);
        //底部按钮
        this.b1 = (Button) findViewById(R.id.translation);
        this.b2 = (Button) findViewById(R.id.dictionary);
        this.b3 = (Button) findViewById(R.id.index);

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        mydbhelper = new MyDatabaseHelper(this, "Mysentence.db", null, 1);
        SQLiteDatabase db = mydbhelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from Words order by random() limit 1", null);
        cursor.moveToFirst();
        cid = cursor.getInt(cursor.getColumnIndex("id"));

        //设置状态栏透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //初始化控件
        initView();

        txt.setText(cursor.getString(cursor.getColumnIndex("contain")));
        cursor.close();
        //初始化底部按钮事件
        initEvent();

    }
    private void initEvent() {
        //为按钮注册监听事件
        b1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent = new Intent(Main_Activity.this, TranslateActivity.class);
                startActivity(intent);
                finish();
            }
        });

        b2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent = new Intent(Main_Activity.this, DictionaryActivity.class);
                startActivity(intent);
                finish();
            }
        });

        del.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = mydbhelper.getWritableDatabase();
                db.delete("Words", "id = ?", new String[]{String.valueOf(cid)});
                Toast.makeText(Main_Activity.this,"delete success", Toast.LENGTH_SHORT).show();

                Cursor cursor = db.rawQuery("select * from Words order by random() limit 1", null);
                cursor.moveToFirst();
                txt.setText(cursor.getString(cursor.getColumnIndex("contain")));
                cursor.close();

            }
        });
        change.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                int t = cid;
                SQLiteDatabase db = mydbhelper.getWritableDatabase();
                Cursor cursor = db.rawQuery("select * from Words order by random() limit 1", null);
                if (cursor.getColumnCount() > 1) {
                    while (t == cid) {
                        cursor = db.rawQuery("select * from Words order by random() limit 1", null);
                        cursor.moveToFirst();
                        t = cursor.getInt(cursor.getColumnIndex("id"));
                    }
                    cid = t;
                    txt.setText(cursor.getString(cursor.getColumnIndex("contain")));
                }
                cursor.close();
            }
        });
        write.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent = new Intent(Main_Activity.this, AddActivity.class);
                startActivity(intent);
                //finish();
            }
        });
    }
}

